<?php
/**
 * 
 * @author hushuai
 *
 */
class IndexAction extends CommonAction{
	
	public function test(){
		$this->display();
	}
	
	//首页
	public function index(){
		
		cookie(C('USER_SELECT_CITY'),'');
		
		//获取用户所在城市
		$data  = getCityInfo();
		$query = addressJsonDecode();

		$this->assign("query",$query);
		$this->assign("data",$data);
		$this->display();
	}
	
	//异步获取通过浏览器确定的用户具体位置
	public function getAddress(){
		
		$location = $this->_param("location");
		$addComp  = $this->_param("addComp");
		$map      = addressJsonDecode();
		
		$map['pointlng'] = $location['lng'];
		$map['pointlat'] = $location['lat'];
		if($addComp){
			$map['adds'] = $addComp['province'].' '.$addComp['city'].' '.$addComp['district'].' '.$addComp['street'].' '.$addComp['streetNumber'];
			$map['type'] = "1";
		}
		
		addressJsonEncode($map);
	}
	
	//获取常用地址的经纬度
	public function adds(){
		
		$map   = addressJsonDecode();
		$adds  = $this->_param("adds","trim","");
		$addid = $this->_param("addid","intval",0);
		
		if($addid){
			
			$cat['id'] = $addid;
			$data = sendRest("RMS/Site/detail",$cat);
			$map['pointlng'] = $data['data']['longitude'];
			$map['pointlat'] = $data['data']['latitude'];
			$map['addid']    = $addid;
			$map['siteid']   = $addid;
			$map['adds']     = $data['data']['address'];
			$map['enadds']   = $data['data']['enaddress'];
		}else{
			$map['enadds'] = $adds;
		}
		addressJsonEncode($map);
	}
	
	//选择地址页面
	public function address(){
		
		$map  = addressJsonDecode();
			
		$lang = getLang();
		$city = C('CITYID');
		
		if(IS_AJAX){
			
			$cat['keywords'] = $this->_param("value");
			$cat['cityid']   = $map['cityid'];
			if(!$cat['cityid'] ){
				$cat['cityid']  = 867;
				}
			$json = sendRest("RMS/Site/select",$cat);
			$html = "";
			foreach($json['data'] as $k=>$v){
				if($lang=='en-us'){
					$html .= "<dd><span did=".$v['id'].">".$v['enaddress']."</span></dd>";
				}else{
					$html .= "<dd><span did=".$v['id'].">".$v['address']."</span></dd>";
				}
			}
	         $this->ajaxReturn($html);
		}else{
			
			//设置用户选择城市session
			if($this->_param("city")){
				$map['city']   = $this->_param("city");
				$map['cityid'] = $this->_param("cityid");
				$map['encity'] = $city[$map['cityid']][0];
				$map['zhcity'] = $city[$map['cityid']][1];
				addressJsonEncode($map);
			}
			
			$query = addressJsonDecode();
			if(empty($query['cityid'])){
				$this->assign("noneCity","1");//没有选择城市
			}
			
			$this->assign("city",$query);
			$this->display();
		}
	}
	
	//我的地址
	public function my_address(){

		$map['id'] = cookie(C('USER_AUTH_KEY'));
		if($map['id']){
			
			$json = sendRest('RMS/Address/getAddressByUser',$map);
			$this->assign('address',$json['data']);
			$this->display();
		}else{
			$this->error('请先登录',U('Public/login'));
		}
	}
	
	
	public function ditu(){
		$this->display();
	}
	
}
